/**
 * Independent time operation tool to facilitate subsequent switch to dayjs
 */
import moment from 'moment';

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DATE_FORMAT = 'YYYY-MM-DD ';

export function formatToDateTime(date: moment.MomentInput = undefined, format = DATE_TIME_FORMAT): string {
  return moment(date).format(format);
}

export function formatToISOTime(date: moment.MomentInput = undefined): string {
  return moment(date).toISOString();
}

export function formatToDate(date: moment.MomentInput = undefined, format = DATE_FORMAT): string {
  return moment(date).format(format);
}

export const dateUtil = moment;

export const stampToDate = (numberTime: number | string) => {
  let num = numberTime;
  if (typeof numberTime == 'string') {
    num = Number(numberTime);
  }
  const date = new Date(num);
  const year = date.getFullYear();
  const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  const h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  const m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  const res = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
  return res;
};
